#ifndef StyledText_First
#define StyledText_First
#ifdef __GNUG__
#pragma interface
#endif

#include "GapText.h"
#include "TextStyles.h"

class StyledTextIter;
class RunArray;

//---- StyledText --------------------------------------------------------------

class StyledText: public GapText {
public:
	MetaDef(StyledText);

	StyledText(int size= 32, Font *fd= gSysFont, TxtParaAlign= eTxtParaLeft);
	StyledText(byte *buf, int len= -1, Font *fd= gSysFont, TxtParaAlign= eTxtParaLeft);
	StyledText(Font *fd, const char *format, ...);
		// to create a text in different faces, specified in a string
		// 'format' can contain a format specifications as strprintf.
		// Extensions are: @B, @I, @O, @S, @P, @U with the meaning to change the
		//                 face of the text to
		//                 Bold,Italic,Outline,Shadow,Plain,Underline
		// example:
		// char * filename;
		// StyledText ex("File @B%s@B does not exist", filename);
	~StyledText();
	void InitNew();

	RunArray *GetCharStyles();
	RunArray *SetCharStyles(RunArray *st);
	RunArray *GetParaStyles();
	RunArray *SetParaStyles(RunArray *st);

	//---- editing
	void Cut(int from, int to);
	void ReplaceWithStr(byte *str, int len = -1);

	//---- style manipulation
	virtual void SetCharStyle(TxtCharProp m, int f, int t, const CharStyleSpec &st);
	virtual void SetParaStyle(TxtParaProp m, int f, int t, const ParaDesc &pd);
	CharStyle *GetCharStyle(int at);
	ParaStyle *GetParaStyle(int at);

	virtual void ReplaceCharStyles(RunArray *styles, int from, int to);
	virtual void ReplaceParaStyles(RunArray *styles, int from, int to);
	virtual void ResetCurrentCharStyle();
	CharStyle *GetCurrentCharStyle();
		// 0 == no current style

	//---- activation/passivation
	OStream& PrintOn(OStream&);
	IStream& ReadFrom(IStream&);
	IStream& ReadFromAsPureText(IStream &s, long sizeHint= -1);

	void CheckInvariant();
	void Dump();

protected:
	void SetFStringVL(char *fmt, va_list va);
	void ReplaceRange(int from, int to, Text *src, int sfrom, int sto);

private:
friend StyledTextIter;

	int GetNextFontChange(int at, CharStyle *&sp);
	void InitStyles(CharStyle *cs, ParaStyle *ps);

	void BuildStylesFromString(CharStyle *initialStyle, char *buf);
	void CutParaStyles(int from, int to, int sz);
	void CutStyles(int from, int to, int sz);
	void PasteParaStyles(int from, int to, RunArray *pr, int sfrom, int sto);
	void AddChars(int at, int sz);

private:
	RunArray *charStyles, *paraStyles;
	CharStyle *styleHere;   // style used during next text insertion
	bool applyStyleHere;    // will we use styleHere ?
};

#endif
